package com.l2qq.common.kit;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 公用的jsonObj解析，直接解析成Kv
 */
public class CondKit {
    public static Kv getCond(JSONObject jsonObject){
        Kv kv = Kv.create();
        //公用 第几页
        if (jsonObject.containsKey("currentPage")) {
            int pageNumber = jsonObject.getInteger("currentPage");
            kv.set("pageNumber", pageNumber);
        } else {
            kv.set("pageNumber", 1);
        }
        if (jsonObject.containsKey("pageNumber")) {
            int pageNumber = jsonObject.getInteger("pageNumber");
            kv.set("pageNumber", pageNumber);
        } else {
            kv.set("pageNumber", 1);
        }
        //公用 每页显示数量
        if (jsonObject.containsKey("pageSize")) {
            int pageSize = jsonObject.getInteger("pageSize");
            kv.set("pageSize", pageSize);
        } else {
            kv.set("pageSize", 50);
        }
        //公用 是否精准查询
        if (jsonObject.containsKey("isAccurate")) {
            boolean isAccurate = jsonObject.getBooleanValue("isAccurate");
            kv.set("isAccurate", isAccurate);
        }else{
            kv.set("isAccurate", false);
        }
        if (jsonObject.containsKey("pickerDate") && jsonObject.get("pickerDate") != null && jsonObject.getString("pickerDate").contains("[") && jsonObject.getJSONArray("pickerDate").size() == 2) {
            SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd");
            JSONArray pickerDate = jsonObject.getJSONArray("pickerDate");
            String startDate = simpleFormatter.format(pickerDate.getDate(0)) + " 00:00:00";
            String endDate = simpleFormatter.format(pickerDate.getDate(1)) + " 23:59:59";
            kv.set("startDate", startDate).set("endDate", endDate);
        }

        /*if (jsonObject.containsKey("formData") && jsonObject.getJSONObject("formData") != null) {
            JSONObject formData = jsonObject.getJSONObject("formData");
            //全部 未审核 已审核
            if (formData.containsKey("CNStateManCheck")) {
                String CNStateManCheck = formData.getString("CNStateManCheck");
                kv.set("CNStateManCheck", CNStateManCheck);
            }
            //全部 未收款 已收款
            if (formData.containsKey("proceedsCheck")) {
                String proceedsCheck = formData.getString("proceedsCheck");
                kv.set("proceedsCheck", proceedsCheck);
            }
            //含税，不含税 销售开单
            if (formData.containsKey("taxCheck")) {
                String taxCheck = formData.getString("taxCheck");
                kv.set("taxCheck", taxCheck);
            }
        }*/


        if (jsonObject.containsKey("filterData")) {
            JSONObject filterData = jsonObject.getJSONObject("filterData");
            /*if (filterData.size() > 0) {
                //存货成本核算v3 规格、类型
                if (filterData.containsKey("specification")) {
                    kv.set("specification", filterData.getString("specification"));
                }
                if (filterData.containsKey("category")) {
                    kv.set("category", filterData.getString("category"));
                }
                if (filterData.containsKey("shape")) {
                    kv.set("shape", filterData.getString("shape"));
                }
            }*/
            kv.set("filterData",filterData);
        }
        if (jsonObject.containsKey("sortData")) {
            JSONObject sortData = jsonObject.getJSONObject("sortData");
            if (sortData.size() > 0) {
                kv.set("sortData", sortData);
            }
        }
        return kv;
    }
}
